using UnityEngine;
using System.Collections;

public enum MusicType
{
    peace,
    battle
}
public class FXSoundManager : MonoBehaviour
{
	public AudioClip currentClip;
    public AudioClip[] clipList;
    public float length;
    public static FXSoundManager instance;
    public AudioSource peaceSource;
    public AudioSource battleSource;
    public float volume = 1.0f;

    [SerializeField]
    private MusicType type = MusicType.peace;

    public void Awake()
    {
        instance = this;
    }

	void Start ()
	{	
        //currentClip = clipList[0];
        //AudioSource.PlayClipAtPoint(currentClip, transform.position);
        //length = currentClip.length;
        //PlayCircling();
	}

    void PlayCircling()
    {
        AudioSource.PlayClipAtPoint(currentClip, transform.position);
        int index = Random.Range(0,clipList.Length);
        currentClip = clipList[index];
        length = currentClip.length;
        Invoke("Play",length);
    }

    [SerializeField]
    private float peaceToBattleBlankTime = 0.5f;

    [SerializeField]
    private float battleToPeaceBlankTime = 1.0f;
    
    [SerializeField]
    private float peaceFadeInRate = 0.1f;

    [SerializeField]
    private float peaceFadeOutRate = 0.5f;
    
    [SerializeField]
    private float battleFadeInRate = 0.5f;

    [SerializeField]
    private float battleFadeOutRate = 0.1f;

    public bool isBattleToPeace = false;
    public bool isPeaceToBattle = false;

    public void ChangeVolume(float volume)
    {
        this.volume = volume;
        battleSource.volume = volume;
        peaceSource.volume = volume;
    }
    
    IEnumerator BattleToPeace( )        
    {
        isBattleToPeace = true;
        isPeaceToBattle = false;

        while( battleSource.volume > 0.1f * volume )
        {            
            battleSource.volume = Mathf.Lerp(battleSource.volume,0,battleFadeOutRate * Time.deltaTime ) * volume; 
            yield return null;            
        }
        battleSource.volume = 0.0f;
        battleSource.Stop();

        yield return new WaitForSeconds(battleToPeaceBlankTime);

        peaceSource.Play();
        while( peaceSource.volume < 0.9f * volume ) 
        {
            peaceSource.volume = Mathf.Lerp(peaceSource.volume , 1.0f, peaceFadeInRate * Time.deltaTime) * volume;
            yield return null;
        }
        peaceSource.volume = 1.0f * volume;
        isBattleToPeace = false;
    }
    
    IEnumerator PeaceToBattle( )        
    {
        isPeaceToBattle = true;
        isBattleToPeace = false;
        while( peaceSource.volume > 0.1f * volume)
        {            
            peaceSource.volume = Mathf.Lerp(peaceSource.volume,0,peaceFadeOutRate * Time.deltaTime ) * volume; 
            yield return null;            
        }
        peaceSource.volume = 0.0f;
        peaceSource.Stop();

        yield return new WaitForSeconds(peaceToBattleBlankTime);

        battleSource.Play();
        while( battleSource.volume < 0.9f * volume) 
        {
            battleSource.volume = Mathf.Lerp(battleSource.volume , 1.0f, battleFadeInRate * Time.deltaTime) * volume;
            yield return null;
        }
        battleSource.volume = 1.0f * volume;
        isPeaceToBattle = false;
    }

    //will change to FSM to control ,A->B, blank, B->A
    public void CrossFade(MusicType type)
    {
        if(this.type == type)
        {
            return;
        }
        this.type = type;
        if(type == MusicType.peace)
        {
            if(isBattleToPeace) return;
            StartCoroutine("BattleToPeace");

        }
        else if(type == MusicType.battle)
        {
            if(isPeaceToBattle) return;
            StartCoroutine("PeaceToBattle");
        }
    }

	 
}

